﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.News.NewsContent;
using BCService.Common.News.NewsContentService;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Management.Common.News.NewsContent
{
    /// <summary>
    /// 新闻内容控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class NewsContentsController : ManagementBaseController
    {
        private readonly INewsContentService newsContentService;
        /// <summary>
        /// 新闻内容控制器
        /// </summary>
        public NewsContentsController(INewsContentService newsContentService)
        {
            this.newsContentService = newsContentService;
        }
        /// <summary>
        /// 获取新闻内容列表（分页）
        /// </summary>
        /// <param name="contentCategoryId">分类名称</param>
        /// <param name="title">标题</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">开始时间</param>
        /// <param name="keyWord">关键字</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页</param>
        /// <param name="size">每页条数</param>
        /// <response code="200">带有分页信息的银行账户列表。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<NewsContentDto>), 200)]
        public IActionResult Get([FromQuery(Name = "content_category_id")] int? contentCategoryId,
            [FromQuery(Name = "key_word")]string keyWord,
            [FromQuery(Name = "title")]string title,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.newsContentService.GetNewsPagingList(contentCategoryId, title, keyWord, start, end, true, ascending, index, size));
        }

        /// <summary>
        /// 获取一条新闻内容
        /// </summary>
        /// <param name="newsContentId">新闻内容ID</param>
        /// <returns></returns>
        [HttpGet("{news_content_id}")]
        [ProducesResponseType(typeof(NewsContentDto), 200)]
        public IActionResult Get([FromRoute(Name = "news_content_id")]long newsContentId)
        {
            return this.Ok(this.newsContentService.GetNewsContentById(newsContentId, true));
        }

        /// <summary>
        /// 创建一个新的新闻内容
        /// </summary>
        /// <returns></returns>
        /// <response code="201">新创建的Banner信息。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        [HttpPost]
        [Log("添加新闻内容")]
        [ProducesResponseType(typeof(NewsContentDto), 201)]
        public IActionResult Post([FromBody]NewsContentRequestDto dto)
        {
            var result = this.newsContentService.Create(dto, this.Admin.AdminId, this.Admin.FullName);

            return this.Created(string.Format("/api/v1/sim/NewsContent/{0}", result.NewsContentId), result);
        }

        /// <summary>
        /// 修改一个新的新闻内容
        /// </summary>
        /// <returns></returns>
        /// <response code="201">新创建的Banner信息。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        [HttpPut("{news_content_id}")]
        [Log("修改新闻内容")]
        [ProducesResponseType(typeof(NewsContentDto), 200)]
        public IActionResult Update([FromRoute(Name = "news_content_id")]long newsContentId, [FromBody]NewsContentRequestDto dto)
        {
            return this.Ok(this.newsContentService.Update(newsContentId, dto, this.Admin.AdminId));
        }

        /// <summary>
        /// 删除一个新的新闻内容
        /// </summary>
        /// <returns></returns>
        /// <response code="204">刪除新闻內容成功。</response>
        [HttpDelete("{news_content_id}")]
        [Log("删除新闻内容")]
        public IActionResult Delete([FromRoute(Name = "news_content_id")]long newsContentId)
        {
            this.newsContentService.Delete(newsContentId);
            return NoContent();
        }

        #region 启用/禁用新闻内容
        /// <summary>
        ///  启用/禁用新闻内容
        /// </summary>
        /// <param name="newsContentId">新闻内容Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用新闻内容成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{news_content_id}/{enabled}")]
        [Log("启用/禁用新闻内容")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "news_content_id")]long newsContentId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.newsContentService.Enabled(newsContentId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.newsContentService.Disabled(newsContentId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}
